public class MySingleList {
    public void ListNode() {

    }
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
        public ListNode(){

        }
    }


    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        ListNode cur = head;
        int size = 0;
        if (cur == null) {
            return 0;
        }
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public void clear(){
        head = null;
    }

    public boolean contains(int key){
        ListNode cur = head;
        if(head == null){
            return false;
        }
        while(cur != null){
            if(cur.val == key){
                return true;
            }else{
                cur = cur.next;
            }
        }
        return false;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
        }else{
            node.next = head;
            head = node;
        }
    }
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
            node.next = null;
        }
        while(cur != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    public boolean addIndex(int index,int data){
        ListNode cur = head;
        ListNode node = new ListNode(data);
        if(head == null){
            return false;
        }
        int size = size();
        if(size < index){
            return false;
        }else {
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            node.next = cur.next;
            cur.next = node;
            return true;
        }
    }

    public void remove(int key){
        if(head == null) {
            return;
        }
        //单独删除头节点
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        if(cur == null) {
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }


    private ListNode searchPrev(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    public void removeAllKey(int key){
        if(head == null) {
            return;
        }
        //单独删除头节点
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        while(cur != null){
        if(cur == null) {
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
        cur = cur.next;
        }
    }



    /*
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }*/
    public class Partition {
        public ListNode partition(ListNode pHead, int x) {
            ListNode  be = null, bs = null, as = null, ae = null;
            ListNode cur = pHead;
            while (cur != null) {
                if (cur.val < x) {
                    if (bs == null) {
                        be = cur;
                        bs = cur;
                    }
                    be.next = cur;
                    be = be.next;
                }
                if (cur.val > x) {
                    if (as == null) {
                        ae = cur;
                        as = cur;
                    }
                    ae.next = cur;
                    ae = ae.next;
                }
                cur = cur.next;
            }
            if(bs == null){
                return as;
            }
            if(as == null){
                return bs;
            }
            be.next = as;
            return bs;
        }
    }

    /**
     * 选择排序：
     *
     * @param array
     */
    public static void selectSort(int[] array) {
        int i = 0;
        for (; i < array.length; i++) {
            int minIndex = i;
            for(int j = i+1;j< array.length;j++){
                if(array[j]<array[i]){
                    minIndex = i;
                }
            }
            int tmp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = tmp;
        }
    }
}

